<template>
	<view>
				<view :style="{ height: statusBarHeight + 'px' }"></view>
				<view>
						<!-- 搜索框 -->
						<view class="search-box">
							<uni-search-bar 
								@input="input" 
								:radius="100" 
								cancel-button="none"
								placeholder="搜索更多帖子"
								bgColor="#f8f4e9"
								placeholderStyle="color: #a68a64;"
							></uni-search-bar>
						</view>
					
						<!-- 搜索结果列表 -->
						<view class="sugg-list" v-if="searchResults.length !== 0">
							<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="toDetailPage(item.postId)">
								<view class="goods-name">{{item.postTitle}}</view>
								<uni-icons type="arrowright" size="20" color="#7C191E"></uni-icons>
							</view>
						</view>
						<!-- 搜索历史 -->
						<view class="history-box" v-else>
							<!-- 标题区域 -->
							<view class="history-title">
								<text class="title-text">搜索历史</text>
								<uni-icons type="trash" size="20" color="#7C191E" @click="clean"></uni-icons>
							</view>
							<!-- 列表区域 -->
							<view class="history-list">
								<uni-tag 
									:text="item" 
									v-for="(item,index) in historyList" 
									:key="index"
									type="primary"
									:inverted="true"
									:custom-style="{
										backgroundColor: '#f8f4e9',
										color: '#7C191E',
										borderColor: '#d4a373'
									}"
								></uni-tag>
							</view>
						</view>
				
						
					</view>
				
	</view>
	</template>

<script>
	export default {
		data() {
			return {
				kw: '', // 用户输入的关键词
					statusBarHeight: 0,
				allResults: [], // 示例数据
				searchResults: [], // 搜索结果
				//搜索历史的数组
				historyList: []
			};
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync();
					this.statusBarHeight = systemInfo.statusBarHeight || 0;
			//提前拿到数
				uni.request({
					url: this.$BASE_URL.baseUrl + '/community/post/list',
					
					success: (res) => {
							this.allResults = res.data.rows
							console.log(this.allResults)
					}
				})
			
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					// console.log(e)
					// 拿到关键字
					this.kw = e
					//送入搜索历史
					this.historyList.push(this.kw)
					//调用展示数据方法
					this.handleSearch()

				}, 500)
			},
			// 处理搜索逻辑
			handleSearch() {
				const keyword = this.kw.trim(); // 去除关键词两端的空格
				if (!keyword) {
					this.searchResults = []; // 如果关键词为空，则清空搜索结果
					return;
				}

				// 使用关键词对数据进行模糊查询
				const results = this.allResults.filter(item => {
					// 在所有属性中搜索关键词
					for (const key in item) {
						if (Object.prototype.hasOwnProperty.call(item, key)) {
							if (typeof item[key] === 'string' && item.postTitle.indexOf(keyword) !== -1) {
								return true; // 如果找到匹配的关键词，则返回 true
							}
						}
					}
					return false; // 如果没有找到匹配的关键词，则返回 false
				});

				this.searchResults = results;
			},
			saveSearchHistory() {
				//1.将Array 数组转化为set对象
				const set = new Set(this.historyList)
				// 2.调用set对象的delete方法 ,移除对应的元素
				set.delete(this.kw)
				// 3.调用set对象 的add方法,向set中添加元素
				set.add(this.kw)
				// 4.将set对象转化为Array数组
				this.historyList = Array.from(set)
				//调用uni.setStorageSync(key,value) 加载本地存储 将搜索历史记录持久化存储到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clean() {
				this.historyList = []
				// uni.setStorageSync('kw', '[]')
			},
			toDetailPage(postId) {
				uni.navigateTo({
					url: `/pages/community/detailPage/detailPage?postId=${postId}`
				})
			},
		},
	};
</script>

<style lang="scss" scoped>
	.search-container {
		padding: 20rpx;
		background-color: #fff;
		min-height: 100vh;
	}
	
	/* 搜索框样式 */
	.search-box {
		padding: 20rpx 0;
		position: sticky;
		top: 0;
		background-color: #fff;
		z-index: 999;
	}
	
	/* 搜索结果列表 */
	.sugg-list {
		padding: 0 20rpx;
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
		
		.sugg-item {
			padding: 30rpx 0;
			border-bottom: 1rpx solid #f0e6d6;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			&:last-child {
				border-bottom: none;
			}
			
			.goods-name {
				font-size: 28rpx;
				color: #3a2818;
				flex: 1;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 20rpx;
			}
		}
	}
	
	/* 搜索历史 */
	.history-box {
		padding: 20rpx;
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
		
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f0e6d6;
			
			.title-text {
				font-size: 32rpx;
				color: #5e3a1e;
				font-weight: bold;
			}
		}
		
		.history-list {
			padding: 20rpx 0;
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
		}
	}
</style>